package com.zl.tby.service.impl;

import com.zl.tby.core.base.bean.TbyPageResult;
import com.zl.tby.core.base.enums.BusinessIdentifyingEnum;
import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.core.common.R;
import com.zl.tby.dto.TbyMonitoringStationQuery;
import com.zl.tby.model.TbyMonitoringStation;
import com.zl.tby.model.TbyUavInfo;
import com.zl.tby.repository.TbyMonitoringStationRepo;
import com.zl.tby.service.ITbyFileBusinessService;
import com.zl.tby.service.ITbyMonitoringStationService;
import com.zl.tby.vo.TbyMonitoringStationForm;
import com.zl.tby.vo.TbyResearchActivitiesForm;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.data.annotation.Tran;
import org.noear.wood.IPage;
import org.noear.wood.annotation.Db;
import org.noear.wood.impl.IPageImpl;
import org.noear.wood.utils.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 监测站点接口实现类
 * @author changpeng
 * @version 1.0
 * @since 2024/8/6 20:22
 */
@Component
public class TbyMonitoringStationServiceImpl implements ITbyMonitoringStationService {

    @Db
    private TbyMonitoringStationRepo tbyMonitoringStationRepo;

    @Override
    @Tran
    public R<TbyMonitoringStation> add(TbyMonitoringStationForm form) {
        TbyMonitoringStation entity = form.createEntity();
        entity.setCreateBy(ToolsUtil.getLoginUserId());
        entity.setCreateTime(LocalDateTime.now());
        long id = tbyMonitoringStationRepo.insert(entity, true);
        entity.setId(id);
        return R.ok(entity);
    }

    @Override
    @Tran
    public R<TbyMonitoringStation> update(TbyMonitoringStationForm form) {
        TbyMonitoringStation entity = this.get(form.getId());
        if (entity == null) {
            return R.failed("数据不存在");
        }
        form.updateEntity(entity);
        entity.setUpdateBy(ToolsUtil.getLoginUserId());
        entity.setUpdateTime(LocalDateTime.now());
        tbyMonitoringStationRepo.updateById(entity, false);
        return R.ok(entity);
    }

    @Override
    public int delete(Long id) {
        return tbyMonitoringStationRepo.deleteById(id);
    }

    @Override
    public TbyMonitoringStation get(Long id) {
        return tbyMonitoringStationRepo.selectById(id);
    }

    @Override
    public TbyPageResult<TbyMonitoringStationForm> page(TbyMonitoringStationQuery query) {
        IPage<TbyMonitoringStation> iPage = tbyMonitoringStationRepo.selectPage(query.getOffset(), query.getSize(), mp -> {
            mp.whereTrue()
                    .andIf(StringUtils.isNotEmpty(query.getDeviceName()), "device_name like ?", "%" + query.getDeviceName() + "%")
                    .andIf(StringUtils.isNotEmpty(query.getDeviceCode()), "device_code = ?", query.getDeviceCode());
            if (StringUtils.isNotEmpty(query.getOrder())) {
                mp.orderBy(query.getOrder());
            }
        });
        List<TbyMonitoringStationForm> formList = new ArrayList<>();
        iPage.getList().forEach(item -> {
            formList.add(TbyMonitoringStationForm.recordConvert(item));
        });
        return new TbyPageResult(new IPageImpl(formList, iPage.getTotal(), iPage.getSize()), query);
    }

    @Override
    public List<TbyMonitoringStationForm> list(TbyMonitoringStationQuery query) {
        List<TbyMonitoringStation> dataList = tbyMonitoringStationRepo.selectList(mapperWhereQ -> {
            mapperWhereQ.whereTrue().andIf(StringUtils.isNotEmpty(query.getDeviceName()), "device_name like ?", "%" + query.getDeviceName() + "%")
                    .andIf(StringUtils.isNotEmpty(query.getDeviceCode()), "device_code = ?", query.getDeviceCode());
            if (StringUtils.isNotEmpty(query.getOrder())) {
                mapperWhereQ.orderBy(query.getOrder());
            }
        });
        List<TbyMonitoringStationForm> resultList = new ArrayList<>();
        dataList.forEach(item -> {
            resultList.add(TbyMonitoringStationForm.recordConvert(item));
        });
        return resultList;
    }

    /**
     * 根据设备编号查出设备id
     * @author AnWeiNan
     * @date 2024/11/6 上午9:30
     */
    @Override
    public List<TbyMonitoringStation> selectByDeviceCode(String deviceCode) {
        List<TbyMonitoringStation> dbList = this.tbyMonitoringStationRepo.selectList(mp -> {
            mp.whereEq("device_code", deviceCode);
        });

        // 如果查询到的结果为空，返回一个空列表；否则返回查询结果
        return dbList.isEmpty() ? Collections.emptyList() : dbList;
    }

    @Override
    public R<Boolean> checkDeviceCode(Long id, String deviceCode) {
        if (StringUtils.isEmpty(deviceCode)) {
            return R.ok(false);
        }
        List<TbyMonitoringStation> dbList = this.tbyMonitoringStationRepo.selectList(mp -> {
            mp.whereEq("device_code", deviceCode);
        });
        for (TbyMonitoringStation data : dbList) {
            if (id == null && data.getDeviceCode().equals(deviceCode)) {
                return R.ok(true);
            } else if (id != null && data.getDeviceCode().equals(deviceCode) && id.compareTo(data.getId()) != 0) {
                return R.ok(true);
            }
        }
        return R.ok(false);
    }

}
